package com.jservice.api.service.upp;

import com.alibaba.fastjson.JSONObject;
import com.jservice.api.MsgCode;
import com.jservice.api.UppClient;
import com.jservice.api.dto.Request;
import com.jservice.api.dto.Response;
import com.jservice.api.dto.ResponseHeader;
import com.jservice.api.service.BaseService;
import com.jservice.api.service.IService;
import com.jservice.api.smkAppUser.AppUserKit;
import com.jservice.model.SmkAppUser;
import com.jservice.web.smkSn.SnService;
import com.wondersgroup.upp.api.core.UPPlatformRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 圈存申请
 *
 * @author cwvian
 * @version 1.0
 * @date 2017/7/3
 */
@Service("upp2028")
public class Upp2028 extends BaseService implements IService {

	private String SERVER_NAME = "upp.trade.credit.for.load.apply";

	@Resource
	private SnService snService;

	@Override
	public Response excute(Request request, Response response, ResponseHeader header, String method) throws IOException {
		Map paramMap = request.getData();
		String card_no = paramsStringFilter(paramMap.get("card_no"));
		String trans_code = paramsStringFilter(paramMap.get("trans_code"));
		Integer amount = paramsIntegerFilter(paramMap.get("amount"));
		String wallet_no = paramsStringFilter(paramMap.get("wallet_no"));
		String wallet_seq = paramsStringFilter(paramMap.get("wallet_seq"));
		String wallet_balance = paramsStringFilter(paramMap.get("wallet_balance"));
		String wallet_random = paramsStringFilter(paramMap.get("wallet_random"));
		String date = paramsStringFilter(paramMap.get("date"));
		String time = paramsStringFilter(paramMap.get("time"));
		String wallet_mac = paramsStringFilter(paramMap.get("wallet_mac"));

		String trade_no = paramsStringFilter(paramMap.get("trade_no"));
		SmkAppUser smkAppUser = AppUserKit.getCurrent(request);
		if (smkAppUser == null) {
			return renderFAIL(MsgCode.Code_1007, response, header);
		}
		String out_trade_no = snService.generate(SnService.Type.order);
		Map data = new HashMap();
		data.put("card_no", card_no);
		data.put("trans_code",trans_code);
		data.put("out_trade_no",out_trade_no);
		data.put("amount",amount);
		data.put("auth_type","032");
		data.put("wallet_no",wallet_no);
		data.put("wallet_seq",wallet_seq);
		data.put("wallet_balance",wallet_balance);
		data.put("wallet_random",wallet_random);
		data.put("date",date);
		data.put("time",time);
		data.put("trans_type","02");
		data.put("psam_no","112233445566");
		data.put("wallet_id","00");
		data.put("wallet_mac",wallet_mac);
		data.put("trade_no",trade_no);
		UPPlatformRequest uRequest = getUPRequest(SERVER_NAME, request.getHeader());
		uRequest.setBiz_content(sortParam(data));
		UppClient client = UppClient.getInstance();
		JSONObject jsonObject = client.doRequest(uRequest);
		if ("000000".equals(jsonObject.getString("code"))) {
			jsonObject.remove("code");
			jsonObject.put("out_trade_no",out_trade_no);
			return renderSUC(jsonObject, response, header);
		} else {
			return renderFAIL(MsgCode.Code_1009, "市民卡网关返回异常:" + jsonObject.getString("msg"), response, header);
		}

	}
}